package com.itheima;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.UserService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class UserServiceImpl implements UserService {

    @Override
    public PageInfo<User> findByPage(int currPage, int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<User> info = null;

        try {
//        通过MybatisUtil工厂内获取Sqlsession对象
            sqlSession = MybatisUtil.getSqlSession();
//         通过MybatisUtil工厂内获取dao接口的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
//        执行操作释放资源
//        查询所有之前设置分页参数
            PageHelper.startPage(currPage,pageSize);
//        查询所有
            List<User> list = userDao.findAll();
            info = new PageInfo<>(list);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return info;
    }

    @Override
    public void save(User user) {
        SqlSession sqlSession = null;
//        建立MybatisUtil工厂对象
        try {
            sqlSession = MybatisUtil.getSqlSession();
//        通过mybatisUtil工厂对象获得dao接口的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
//        生成一个唯一的ID ，保存到User
            String id = UUID.randomUUID().toString().replace("-", "");
            user.setId(id);
//            对用户密码进行加密处理
            String password = MD5Util.md5(user.getPassword());
            user.setPassword(password);
//        执行操作
            userDao.save(user);
//        提交事务
            MybatisUtil.commit(sqlSession);
        } finally {
            //        释放资源
            MybatisUtil.close(sqlSession);
        }


    }

    @Override
    public User findById(String id) {
        SqlSession sqlSession = null;
        User user = null;
        try {
//        通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
//        通过MybatisUtil工厂类获取dao接口的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
//        执行操作释放资源
            user = userDao.findById(id);
        } finally {
            MybatisUtil.close(sqlSession);
        }

        return user;
    }

    @Override
    public void update(User user) {
        SqlSession sqlSession =null;
        try {
//           通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
//          通过MybatisUtil工厂类获得dao类对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
//        执行操作
            userDao.update(user);
//        提交事务
            MybatisUtil.commit(sqlSession);
        } finally {
//          释放资源
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = null;
        try {
//        通过MybatisUtil工厂类获得Sqlsession对象
            sqlSession = MybatisUtil.getSqlSession();
//        通过MybatisUtil工厂内获得Dao接口代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
//        执行操作
            Stream.of(ids).forEach(id->{
                    userDao.deleteRoleAssociationByUserId(id);
                userDao.delete(id);
            });
//        提交事务
            MybatisUtil.commit(sqlSession);
        } finally {
            //        释放资源
            MybatisUtil.close(sqlSession);
        }

    }
    @Override
    public List<User> findAll() {
        SqlSession sqlSession = null;
        List<User> list=null;
        try {
//       通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
//         通过MybatisUtil工厂类获取Dao接口代理对象
            UserDao deptDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
//        执行操作释放资源
            list = deptDao.findAll();
        } finally {
            MybatisUtil.close(sqlSession);
        }

        return list;
    }

    @Override
    public void updateUserAndRoles(String userId, String[] checkedRoleIds) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            userDao.deleteUserAndRoles(userId);
            Stream.of(checkedRoleIds).forEach(roleId->{
                userDao.saveUserAndRoles(userId,roleId);
            });
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public User login(String email, String password) {
        SqlSession sqlSession =null;
        User user = null;
        try {
//        通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
//         通过MybatisUtil工厂类获取dao接口的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
//        对密码进行加密
            password = MD5Util.md5(password);
            user = userDao.findByEmailAndPassword(email, password);
        } finally {
            //        执行操作释放资源
             MybatisUtil.close(sqlSession);
        }

        return user;
    }

}
